package com.lianyu.wangyiyun.model;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Chen Yuguang
 * @date 2021/5/20 5:31
 */
public class PlayListInfo {

    private Playlist playlist;

    public Playlist getPlaylist() {
        return playlist;
    }

    public void setPlaylist(Playlist playlist) {
        this.playlist = playlist;
    }

    public static class Playlist {

        private List<Tracks> tracks;

        public void setTracks(List<Tracks> tracks) {
            this.tracks = tracks;
        }

        public List<Tracks> getTracks() {
            return tracks;
        }

        public static class Tracks {

            private String name;
            private long id;
            private List<Ar> ar;
            private Al al;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public long getId() {
                return id;
            }

            public void setId(long id) {
                this.id = id;
            }

            public List<Tracks.Ar> getAr() {
                return ar;
            }

            public void setAr(List<Tracks.Ar> ar) {
                this.ar = ar;
            }

            public Tracks.Al getAl() {
                return al;
            }

            public void setAl(Tracks.Al al) {
                this.al = al;
            }

            public static class Al {

                private String picUrl;

                public String getPicUrl() {
                    return picUrl;
                }

                public void setPicUrl(String picUrl) {
                    this.picUrl = picUrl;
                }

            }

            public static class Ar {

                private String name;

                public String getName() {
                    return name;
                }

                public void setName(String name) {
                    this.name = name;
                }

            }

        }

    }

    /**
     * 将获取的云音乐信息转换为本地音乐信息
     */
    public List<Mp3Info> cloud2local() {
        List<Mp3Info> mp3Infos = new ArrayList<>();
        if (getPlaylist() != null) {
            List<Playlist.Tracks> tracks = getPlaylist().tracks;
            if (tracks != null) {
                for (Playlist.Tracks track : tracks) {
                    Mp3Info mp3Info = new Mp3Info();
                    if (track.id != 0) {
                        mp3Info.setId(track.id);
                        mp3Info.setUrl("https://music.163.com/song/media/outer/url?id=" + track.id + ".mp3");
                    }
                    if (track.name != null) {
                        mp3Info.setTitle(track.name);
                    }
                    if (track.ar != null) {
                        mp3Info.setArtist("");
                        for (Playlist.Tracks.Ar ar : track.ar) {
                            mp3Info.setArtist(mp3Info.getArtist() + ar.name + "，");
                        }
                        mp3Info.setArtist(mp3Info.getArtist().substring(0, mp3Info.getArtist().length() - 1));
                    }
                    if (track.al != null) {
                        if (track.al.picUrl != null) {
                            mp3Info.setAlbum(track.al.picUrl);
                        }
                    }
                    mp3Infos.add(mp3Info);
                }
            }
        }
        return mp3Infos;
    }

}
